<template>
  <!-- 消耗品台账管理表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否启用" prop="enabled">
            <el-select
              style="width: 100%"
              v-if="!props.isReadonly"
              v-model="formData.enabled"
              placeholder="请选择是否启用"
              clearable
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('Enabled')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <span v-else>{{
              dictionaryStore.filterDictName("Enabled", formData.enabled)
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="删除标识" prop="deleted">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.deleted"
              placeholder="请输入删除标识"
              clearable
            />

            <span v-else>{{ formData.deleted }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="父级台账id;默认0" prop="pid">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.pid"
              placeholder="请输入父级台账id;默认0"
              clearable
            />

            <span v-else>{{ formData.pid }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料id" prop="materialId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialId"
              placeholder="请输入物料id"
              clearable
            />

            <span v-else>{{ formData.materialId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="批次号" prop="batchInfo">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.batchInfo"
              placeholder="请输入批次号"
              clearable
            />

            <span v-else>{{ formData.batchInfo }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="批次数量" prop="batchCount">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.batchCount"
              placeholder="请输入批次数量"
              clearable
            />

            <span v-else>{{ formData.batchCount }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="耗材状态;1.待使用；2.使用中；3.待判定；4；待返修；5.报废；"
            prop="status"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.status"
              placeholder="请输入耗材状态;1.待使用；2.使用中；3.待判定；4；待返修；5.报废；"
              clearable
            />

            <span v-else>{{ formData.status }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用设备" prop="equipCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.equipCode"
              placeholder="请输入使用设备"
              clearable
            />

            <span v-else>{{ formData.equipCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="位置" prop="equipPosition">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.equipPosition"
              placeholder="请输入位置"
              clearable
            />

            <span v-else>{{ formData.equipPosition }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="安装位置id" prop="equipPositionId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.equipPositionId"
              placeholder="请输入安装位置id"
              clearable
            />

            <span v-else>{{ formData.equipPositionId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用次数" prop="workCount">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workCount"
              placeholder="请输入使用次数"
              clearable
            />

            <span v-else>{{ formData.workCount }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "EAMCSMLEDGERMANAGEADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  enabled: [],
  deleted: [],
  pid: [],
  materialId: [],
  batchInfo: [],
  batchCount: [],
  status: [],
  equipCode: [],
  equipPosition: [],
  equipPositionId: [],
  workCount: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  enabled: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  lastUpdateTime: "",
  id: "",
  pid: "",
  materialId: "",
  batchInfo: "",
  batchCount: "",
  status: "",
  equipCode: "",
  equipPosition: "",
  equipPositionId: "",
  workLife: "",
  workCount: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
        id,
        pid,
        materialId,
        batchInfo,
        batchCount,
        status,
        equipCode,
        equipPosition,
        equipPositionId,
        workLife,
        workCount,
      } = newVal;
      Object.assign(formData, {
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
        id,
        pid,
        materialId,
        batchInfo,
        batchCount,
        status,
        equipCode,
        equipPosition,
        equipPositionId,
        workLife,
        workCount,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.enabled = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.lastUpdateTime = "";
  formData.id = "";
  formData.pid = "";
  formData.materialId = "";
  formData.batchInfo = "";
  formData.batchCount = "";
  formData.status = "";
  formData.equipCode = "";
  formData.equipPosition = "";
  formData.equipPositionId = "";
  formData.workLife = "";
  formData.workCount = "";
}

const loading = ref(false);

/** 消耗品台账管理保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/eam/csmledgermanage/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
